<?php
namespace app\models;

use Surest\Traits\TreeNode;
use think\Db;
use think\db\Query;
use think\Model;

class Base extends Model
{
    use TreeNode;
    protected $pk;
    
    // 初始化模型
    protected function initialize()
    {
        $this->app_ = ['api'];
        parent::initialize();
        $this->pk = $this->getPk();
    }

    /**
     * 获取单行数据
     * @param array $where 查询条件 格式：[ ['id', '=', 1], ['name', '=', 'fuck'] ]
     * @param string $field
     * @param array $order 排序 格式： ['id' => 'asc', 'start_time' => 'desc'] or ['start_time', 'end_time' => 'desc']
     * @return mixed
     * @throws
     */
    public function getRow(array $where = [],  string $field = '*', array $order = [])
    {
        return empty($where) ? [] : $this->where($where)->field($field)->order($order)->findOrEmpty();
    }

    /**
     * 获取列表数据（分页）
     * @param array $where
     * @param string $field
     * @param array $order
     * @param int $limit
     * @param int $page
     * @return array|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList(array $where = [],  string $field = '*', array $order = [], int $limit = 10, $page = 1)
    {
        return $this->where($where)->field($field)->order($order)->paginate($limit, false, ['page' => $page]);
    }

    public function getAll(array $where = [],  string $field = '*', array $order = [])
    {
        return $this->where($where)->field($field)->order($order)->select();
    }

    public function getInfo(array $where = [],  string $field = '*', array $order = [])
    {
        return $this->where($where)->field($field)->order($order)->find();
    }

    public function getColumnInfo( $where,  string $field, array $order = [], string $key = '')
    {
        return $this->where($where)->order($order)->column($field, $key);
    }

    public function getRowInfoById(int $id = 0, string $field = '*')
    {
        if (!$id) return false;
        return $this->where($this->pk, $id)->field($field)->find();
    }

    public function getValue(array $where = [], string $value = '')
    {
        if (empty($where)) return false;

        return $this->where($where)->value($value);
    }

    public function updateByWhere(array $data = [], array $where = [])
    {
        if (!$data || !$where) return false;

        return $this->where($where)->update($data);
    }

    public function deleteById(int $id = 0)
    {
        if(!$id) return false;

        return $this->where($this->pk, $id)->delete();
    }

    public function deleteByWhere(array $where = [])
    {
        if(empty($where)) return false;

        return $this->where($where)->delete();
    }

    public function getCount(array $where = [])
    {
        return $this->where($where)->count();
    }
	
    public function getTable()
    {
        return $this->table ? $this->table : (new Query())->getTable($this->getName());
    }

    public function getPk()
    {
        $connection = Db::connect($this->connection);
        return $this->pk ? $this->pk : $connection->getPk(['table' => $this->getTable()]);
    }
 
    /**
     * @inheritDoc
     */
    public function offsetExists($offset)
    {
        // TODO: Implement offsetExists() method.
    }

    /**
     * @inheritDoc
     */
    public function offsetGet($offset)
    {
        // TODO: Implement offsetGet() method.
    }

    /**
     * @inheritDoc
     */
    public function offsetSet($offset, $value)
    {
        // TODO: Implement offsetSet() method.
    }

    /**
     * @inheritDoc
     */
    public function offsetUnset($offset)
    {
        // TODO: Implement offsetUnset() method.
    }

    /**
     * @inheritDoc
     */
    public function jsonSerialize()
    {
        // TODO: Implement jsonSerialize() method.
    }
}